package cn.com.git.workflow.service.impl;

import cn.com.git.common.base.BaseServiceImpl;
import cn.com.git.common.model.ResultConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import cn.com.git.common.util.object.MapperFacadeUtils;
import cn.com.git.workflow.dao.WorkFlowConfigDAO;
import cn.com.git.workflow.dao.model.WorkFlowConfig;
import cn.com.git.workflow.service.WorkFlowConfigService;
import cn.com.git.workflow.service.dto.WorkFlowConfigListDTO;
import cn.com.git.workflow.service.dto.WorkFlowConfigDetailDTO;
import cn.com.git.workflow.service.dto.WorkFlowConfigSaveDTO;
import cn.com.git.workflow.service.query.WorkFlowConfigListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* WorkFlowConfig ServiceImpl 接口
* @author zhangzhenfu
* @Description
* @date 2020/5/28
*/
@Transactional
@Service
public class WorkFlowConfigServiceImpl extends BaseServiceImpl implements WorkFlowConfigService {

    @Autowired
    private WorkFlowConfigDAO workFlowConfigDAO;

    /*=================================*/
    /*============ WorkFlowConfig =============*/
    /*=================================*/
    @Override
    public ResultPage<WorkFlowConfigListDTO> listWorkFlowConfig(WorkFlowConfigListQuery query) {
        List<WorkFlowConfigListDTO> data = new ArrayList<>();
        List<WorkFlowConfig> list = workFlowConfigDAO.selectList(query);
            Optional.ofNullable(list).orElse(Lists.newArrayList())
            .stream().forEach(item->{
                WorkFlowConfigListDTO d = new WorkFlowConfigListDTO();
                d.setM(MapperFacadeUtils.map(item,WorkFlowConfigListDTO.WorkFlowConfig.class));
                data.add(d);
            });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<WorkFlowConfigDetailDTO> findWorkFlowConfig(Integer id) {
        WorkFlowConfigDetailDTO d = new WorkFlowConfigDetailDTO();
        WorkFlowConfig workFlowConfig = workFlowConfigDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(workFlowConfig,WorkFlowConfigDetailDTO.WorkFlowConfig.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<WorkFlowConfigSaveDTO> addWorkFlowConfig(WorkFlowConfigSaveDTO saveDTO) {
        Integer id = workFlowConfigDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),WorkFlowConfig.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<WorkFlowConfigSaveDTO> editWorkFlowConfig(WorkFlowConfigSaveDTO saveDTO) {
        workFlowConfigDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),WorkFlowConfig.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteWorkFlowConfig(Integer id) {
        workFlowConfigDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }
}